package com.ruoyi.lowcode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.lowcode.domain.vo.FormModelFieldVo;
import com.ruoyi.lowcode.mapper.LcModelMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lowcode.domain.bo.LcFieldBo;
import com.ruoyi.lowcode.domain.vo.LcFieldVo;
import com.ruoyi.lowcode.domain.LcField;
import com.ruoyi.lowcode.mapper.LcFieldMapper;
import com.ruoyi.lowcode.service.ILcFieldService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 模型字段Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-30
 */
@RequiredArgsConstructor
@Service
public class LcFieldServiceImpl implements ILcFieldService {

    private final LcFieldMapper baseMapper;
    private final LcModelMapper modelMapper;

    /**
     * 查询模型字段
     */
    @Override
    public LcFieldVo queryById(Long fieldId){
        return baseMapper.selectVoById(fieldId);
    }

    /**
     * 查询模型字段列表
     */
    @Override
    public TableDataInfo<LcFieldVo> queryPageList(LcFieldBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LcField> lqw = buildQueryWrapper(bo);
        Page<LcFieldVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询模型字段列表
     */
    @Override
    public List<LcFieldVo> queryList(LcFieldBo bo) {
        LambdaQueryWrapper<LcField> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LcField> buildQueryWrapper(LcFieldBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LcField> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFieldId() != null, LcField::getFieldId, bo.getFieldId());
        lqw.eq(bo.getModelId() != null, LcField::getModelId, bo.getModelId());
        lqw.like(StringUtils.isNotBlank(bo.getFieldAlias()), LcField::getFieldAlias, bo.getFieldAlias());
        lqw.like(StringUtils.isNotBlank(bo.getTableName()), LcField::getTableName, bo.getTableName());
        lqw.isNotNull(bo.getDictionaryId() != null, LcField::getDictionaryId);
        return lqw;
    }

    /**
     * 新增模型字段
     */
    @Override
    public Boolean insertByBo(LcFieldBo bo) {
        LcField add = BeanUtil.toBean(bo, LcField.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFieldId(add.getFieldId());
        }
        return flag;
    }

    /**
     * 修改模型字段
     */
    @Override
    public Boolean updateByBo(LcFieldBo bo) {
        LcField update = BeanUtil.toBean(bo, LcField.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LcField entity){
        entity.setParentModelName("");
        entity.setParentModelFieldAlias("");
        //TODO 做一些数据校验,如唯一约束
        if(entity.getParentModelId() != null){
            entity.setParentModelName(modelMapper.selectVoById(entity.getParentModelId()).getModelName());
        }
        if(entity.getParentModelFieldId() != null){
            entity.setParentModelFieldAlias(baseMapper.selectVoById(entity.getParentModelFieldId()).getFieldAlias());
        }
    }

    /**
     * 批量删除模型字段
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
